<?php
namespace Admin\Controller;
use Think\Controller;
class IndexController extends Controller {
    public function index(){
        $this->show('<style type="text/css">*{ padding: 0; margin: 0; } div{ padding: 4px 48px;} body{ background: #fff; font-family: "微软雅黑"; color: #333;font-size:24px} h1{ font-size: 100px; font-weight: normal; margin-bottom: 12px; } p{ line-height: 1.8em; font-size: 36px } a,a:hover,{color:blue;}</style><div style="padding: 24px 48px;"> <h1>:)</h1><p>欢迎使用 <b>ThinkPHP</b>！</p><br/>版本 V{$Think.version}</div><script type="text/javascript" src="http://ad.topthink.com/Public/static/client.js"></script><thinkad id="ad_55e75dfae343f5a1"></thinkad><script type="text/javascript" src="http://tajs.qq.com/stats?sId=9347272" charset="UTF-8"></script>','utf-8');

    }

    public function _initialize()
    {
        if(!session('admin'))
        {
            $this->redirect("Admin/Login/login");
            //$this->error('无权限！',"http://localhost/appstore/index.php/Admin/Login/login");
        }
    }


    function userList(){
    	$userModel=D('users');
    	$local_users=$userModel->order('id desc')->select();
    	$this->users=$local_users;
    	//this.assign('name',$name);
    	$this->display();
    }


    function bsuserList(){
        // $userModel=D('users');
        // $local_users=$userModel->order('id desc')->select();
        // $this->users=$local_users;
        // //this.assign('name',$name);
        // $this->display();
        $User = M('Users'); // 实例化User对象
        $count      = $User->count();// 查询满足要求的总记录数
        $Page       = new \Think\Page($count,10);// 实例化分页类 传入总记录数和每页显示的记录数(25)

        $show       = $Page->show();// 分页显示输出

       


        $Page->setConfig('header', '<li class="rows">共<b>%TOTAL_ROW%</b>条记录&nbsp;第<b>%NOW_PAGE%</b>页/共<b>%TOTAL_PAGE%</b>页</li>');
    $Page->setConfig('prev', '上一页');
    $Page->setConfig('next', '下一页');
    $Page->setConfig('last', '末页');
    $Page->setConfig('first', '首页');
    $Page->setConfig('theme', '%FIRST%%UP_PAGE%%LINK_PAGE%%DOWN_PAGE%%END%%HEADER%');
    $Page->lastSuffix = false;//最后一页不显示为总页数




       
        // 进行分页数据查询 注意limit方法的参数要使用Page类的属性
        $list = $User->field(true)->limit($Page->firstRow.','.$Page->listRows)->select();
        $this->assign('users',$list);// 赋值数据集
        $this->assign('page',$show);// 赋值分页输出
        $this->display(); // 输出模板





    }


    function abc()
    {
        $this->show('bac<br>');
        $User=D("users")->Select();
        var_dump($User);
        
        //$this->aa=$User;
        //$this->display();
    }

        function users(){
        $userModel=D('users');
        $local_users=$userModel->order('id desc')->select();
        $this->users=$local_users;
        //this.assign('name',$name);
        $this->display();
    }


    function del($id)
    {
        $userModel=D('Users');
        if($userModel->delete($id)){
            //说明删除成功
            $this->success('删除成功');
        }
        else
        {
            $this->error('删除失败');
        }
    }

    function setstar($id)
    {
        $userModel = M('Users');
        $data['isstar']=1;
        $selew['id']=$id;
        if ( $userModel->where($selew)->save($data) )
        {
            $this->success('修改成功');

        }
        else{
            $this->error('修改失败');
        }

    }


    function unstar($id)
    {
        $usersModel=D('Users');
        $usersModel->isstar=0;
        $usersModel->isfirst=0;
        $usersModel->id=$id;
    
        if($usersModel->save()){
            $this->success('成功');
        }
        else
            {
            $this->error('失败');
        }
    }

function setfirst($id)
    {
        $usersModel=D('Users');
        $usersModel->isfirst=1;
        $usersModel->isfirst=1;
        $usersModel->id=$id;
    
        if($usersModel->save()){
            $this->success('成功');
        }
        else
            {
            $this->error('失败');
        }
    }

    function ufirst($id)
    {
        $usersModel=D('Users');
        $usersModel->isfirst=0;
        $usersModel->id=$id;
    
        if($usersModel->save()){
            $this->success('成功');
        }
        else
            {
            $this->error('失败');
        }
    }


    function updateUserInfo($id)
    {
        $usersModel=D('Users');
        $this->user=$usersModel->find($id);
        $this->display();
    }

    function update()
    {
        $userModel=D('Users');
        
        if($userModel->create())
        {

                 if($_FILES['photo']['error']==0)
                {
                     $photoinfo=$this->upload();
                    $userModel->photo=$photoinfo['photo']['savename'];
                }
                if(  $userModel->save() )
                {
                   // var_dump($_POST);
                    $this->success('成功','users');
                }
                else
                {
                  $this->error($userModel->getError());//$userModel->getError());
                }
            }
            else
            {
                 $this->error($userModel->getError());
            }
    }

    public function upload(){
        $upload = new \Think\Upload();// 实例化上传类
        $upload->maxSize = 3145728 ;// 设置附件上传大小
        $upload->exts = array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
        $upload->rootPath = './Public/Uploads/photo/'; // 设置附件上传根目录
       // $upload->savePath = ''; // 设置附件上传（子）目录
        // 上传文件
        $upload->autoSub=false;
        $info = $upload->upload();
        if(!$info) {// 上传错误提示错误信息
        $this->error($upload->getError());
        }else{// 上传成功
       // $this->success('上传成功！');
            return $info;
        }
    }


    function img()
    {
        if($_FILES['photo']['error']==0)
        {


           $photoinfo=$this->upload();//var_dump($photoinfo);
            $fillPath='./Public/Uploads/photo/';
             $filePath=$photoinfo['photo']['rootPath'];
            $oldFileName=$photoinfo['photo']['savename'];
            $oldFileFullName=$fillPath."\\".$oldFileName;
            $newFileName='thumb_'.$oldFileName;
            $newFileFullName=$fillPath."\\".$newFileName;

           
           $img=new \Think\Image();
           $img->open($oldFileFullName);
           $img->thumb(100,100)->save($newFileFullName);
           return $newFileName;

        }
    }

    public function addUser(){
         $userModel=M('Users');
        if($userModel->create()){

       
                   if($_FILES['photo']['error']==0)
                    {
                        $fileInfo=$this->img();
                        $userModel->photo=$fileInfo;
                    }
                    if(  $userModel->add() )
                    {
                        //var_dump($_POST);
                        $this->success('添加成功','users');
                    }
                    else
                    {
                        //var_dump($_POST);
                        $this->error('失败','users');
                    }

                }
        else
        {
            //$this->error("111111111");
             $this->error($userModel->getError());
        }
    }
}